package cn.com.fuhuitong.popup

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebViewClient
import cn.com.fuhuitong.R
import cn.com.fuhuitong.utils.WebViewUtils
import kotlinx.android.synthetic.main.activity_web_view.*
import kotlinx.android.synthetic.main.popup_user_agree.view.*
import razerdp.basepopup.BasePopupWindow

class WebViewPopup(
    context: Context,
    val onConfirm: () -> Unit,
    val onCancel: () -> Unit
) : BasePopupWindow(context) {

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_user_agree)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvPopupConfirm.setOnClickListener {
            this.onConfirm()
        }
        contentView.tvPopupCancel.setOnClickListener {
            this.onCancel()
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    fun setContent(content:String){
        contentView.wvPopupContent.let {
            it.webViewClient = WebViewClient()
            it.settings.domStorageEnabled = true
            it.settings.databaseEnabled = true
            it.settings.javaScriptEnabled = true
            it.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS

            WebViewUtils.loadFullScreenHtml(it, content)
        }
    }

    fun setGoneConfirm(isGone: Boolean): WebViewPopup {
        contentView.tvPopupConfirm.visibility =
            if (isGone) View.GONE else View.VISIBLE
        return this
    }

    fun setGoneCancel(isGone: Boolean): WebViewPopup {
        contentView.tvPopupCancel.visibility =
            if (isGone) View.GONE else View.VISIBLE
        contentView.tvPopupLine.visibility =
            if (isGone) View.GONE else View.VISIBLE
        return this
    }

}